package com.example.client_anna.util.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.client_anna.R;
import com.example.client_anna.util.entity.Home_Image;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * @author cy
 * @email 272884043@qq.com
 * @date 2021/10/19
 */
public class HomeImageFirstAdapter extends RecyclerView.Adapter<HomeImageFirstAdapter.ViewHolder> {

    private Context mContext;
    private List<Home_Image> mImage;

    static class ViewHolder extends RecyclerView.ViewHolder{

        ImageView imageView;
        TextView textView;
        LinearLayout linearLayout;

        public ViewHolder(View view){
            super(view);
            linearLayout = (LinearLayout) view;
            imageView = (ImageView) view.findViewById(R.id.home_image_first);
            textView = (TextView) view.findViewById(R.id.home_text_first);
        }
    }

    public HomeImageFirstAdapter(List<Home_Image> imageList){
        mImage = imageList;
    }

    @NonNull
    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        if(mContext==null){
            mContext = parent.getContext();
        }
        View view = LayoutInflater.from(mContext).inflate(R.layout.home_rec_first,parent,false);
        ViewHolder holder = new ViewHolder(view);
        holder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                Home_Image home_image = mImage.get(position);
                Toast.makeText(mContext,home_image.getDetail(),Toast.LENGTH_SHORT).show();
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull HomeImageFirstAdapter.ViewHolder holder, int position) {
        Home_Image image = mImage.get(position);
        holder.textView.setText(image.getDetail());
        Glide.with(mContext)
                .load(image.getImage())
                .placeholder(R.drawable.explore_placeholder)
                .error(R.drawable.explore_error)
                .dontAnimate()
                .into(holder.imageView);
    }

    @Override
    public int getItemCount() {
        return mImage.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
}
